package linuxbox.status.logic;

import java.util.*;
import java.sql.Timestamp;

import linuxbox.status.bean.*;
import linuxbox.exception.*;
import linuxbox.BoxStatic;

public class StatusManager {
    public static final int ONLINE = 1;
    public static final int BUSY = 2;
    public static final int AWAY = 3;
    public static final int CHECKOUT = 4;

    public static final String ONLINE_STR = "online";
    public static final String BUSY_STR = "busy";
    public static final String AWAY_STR = "away";
    public static final String CHECKOUT_STR = "checkout";

    private static final Map statusMap = new HashMap();
    static {
        statusMap.put(new Integer(ONLINE), ONLINE_STR);
        statusMap.put(new Integer(BUSY), BUSY_STR);
        statusMap.put(new Integer(AWAY), AWAY_STR);
        statusMap.put(new Integer(CHECKOUT), CHECKOUT_STR);
    }

    protected static StatusManager staticInstance = null;

    public static StatusManager getInstance() {
        if (staticInstance == null) {
            staticInstance = new StatusManager();
        }

        return staticInstance;
    }

    private StatusManager() {

    }

    /* User status related methods */
    public static Map getStatusOptions() {
        return statusMap;
    }

    public static String getStatusString(int status) {
        String ret = "unknown";
        switch (status) {
            case ONLINE :
                ret = ONLINE_STR;
            break;

            case BUSY :
                ret = BUSY_STR;
            break;

            case AWAY :
                ret = AWAY_STR;
            break;

            case CHECKOUT :
                ret = CHECKOUT_STR;
            break;
        }
        return ret;
    }

    public StatusObject getStatus(StatusKey key) throws LinuxboxException {
        StatusObject entry = null;

        StatusBean bean = new StatusBean(key);
        bean.load();
        entry = new StatusObject();
        entry.copyFrom(bean);

        return entry;
    }

    public StatusObject getStatus(int id) throws LinuxboxException {
        return getStatus(new StatusKey(id));
    }

    public StatusKey addStatus(StatusObject obj) throws LinuxboxException {
        StatusKey key = null;

        StatusBean bean = new StatusBean();
        bean.create(obj);

        key = new StatusKey(bean.getID());

        return key;
    }

    public void removeStatus(StatusKey key) throws LinuxboxException {
        StatusBean bean = new StatusBean(key);
        bean.remove();
    }

    public void removeStatus(int id) throws LinuxboxException {
        removeStatus(new StatusKey(id));
    }

    public void updateStatus(StatusObject entry) throws LinuxboxException {
        StatusBean bean = new StatusBean(new StatusKey(entry.getID()));
        bean.copyFrom(entry);
        bean.store();
    }

    protected Iterator findStatusBySql(String sql) throws LinuxboxException {
        Iterator it = null;

        StatusBean bean = new StatusBean(null);
        it = bean.findBySql(sql);

        return it;
    }

    public StatusKey findStatusByUserID(int userID) throws LinuxboxException {
        StatusBean bean = new StatusBean();
        StatusKey key = bean.findByUserID(userID);
        return key;
    }

    public boolean isCheckIn(int userID) throws LinuxboxException {
        StatusKey key = findStatusByUserID(userID);
        return (key != null) ? true : false;
    }

    public Iterator getAllStatuses() throws LinuxboxException {
        return findStatusBySql(" ");
    }

    public Iterator getAllStatuses(String sort) throws LinuxboxException {
        return findStatusBySql(sort);
    }

    public void userCheckIn(int userID, Timestamp checkin) throws LinuxboxException {
        BoxStatic.logger.debug("I am in StatusManager.userCheckIn now ... ");
        StatusBean bean = new StatusBean();
        StatusKey key = bean.findByUserID(userID);
        if (key != null) throw LinuxboxException.ALREADY_CHECKIN;
        bean.create(userID);
        bean.updateCheckIn(checkin);
    }

    public void setCheckIn(int userID, Timestamp checkin) throws LinuxboxException {
        StatusBean bean = new StatusBean();
        StatusKey pk = bean.findByUserID(userID);
        bean.init(pk);
        bean.updateCheckIn(checkin);
    }

    public void setCheckOut(int userID, Timestamp checkout) throws LinuxboxException {
        StatusBean bean = new StatusBean();
        StatusKey pk = bean.findByUserID(userID);
        bean.init(pk);
        bean.updateCheckOut(checkout);
        bean.moveStatusToHistory();
    }

    public void setUserStatus(int userID, int status) throws LinuxboxException {
        StatusBean bean = new StatusBean();
        StatusKey pk = bean.findByUserID(userID);
        bean.init(pk);
        bean.updateUserStatus(status);
    }

    public Iterator getOnlineUsers() throws LinuxboxException {
        return null;
    }

    public Iterator getBusyUsers() throws LinuxboxException {
        return null;
    }

    public Iterator getAwayUsers() throws LinuxboxException {
        return null;
    }

    public Iterator getCheckoutUsers() throws LinuxboxException {
        return null;
    }

    public static void main(String[] args) {
        try {
            Class keepStatics = BoxStatic.class;
            //BoxInit.initTestEnv();

            StatusManager sm = StatusManager.getInstance();
            sm.userCheckIn(1, new Timestamp(System.currentTimeMillis()));
            //sm.setCheckIn(1, new Timestamp(System.currentTimeMillis()));
            //sm.setCheckOut(1, new Timestamp(System.currentTimeMillis()));

            //StatusObject so = sm.getStatus(1);
            //BoxStatic.logger.debug(so.dump());

            //sm.setUserStatus(1, 3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* Short message related methods */
    /*public ShortMsgObject getShortMsg(ShortMsgKey key) throws LinuxboxException {
        ShortMsgObject entry = null;

        ShortMsgBean bean = new ShortMsgBean(key);
        bean.load();
        entry = new ShortMsgObject();
        entry.copyFrom(bean);

        return entry;
    }

    public ShortMsgObject getShortMsg(int id) throws LinuxboxException {
        return getShortMsg(new ShortMsgKey(id));
    }

    public ShortMsgKey addShortMsg(ShortMsgObject obj) throws LinuxboxException {
        ShortMsgKey key = null;

        ShortMsgBean bean = new ShortMsgBean();
        bean.create(obj);

        key = new ShortMsgKey(bean.getID());

        return key;
    }

    public void removeShortMsg(ShortMsgKey key) throws LinuxboxException {
        ShortMsgBean bean = new ShortMsgBean(key);
        bean.remove();
    }

    public void removeShortMsg(int id) throws LinuxboxException {
        removeShortMsg(new ShortMsgKey(id));
    }

    public void updateShortMsg(ShortMsgObject entry) throws LinuxboxException {
        ShortMsgBean bean = new ShortMsgBean(new ShortMsgKey(entry.getID()));
        bean.copyFrom(entry);
        bean.store();
    }

    protected Iterator findShortMsgBySql(String sql) throws LinuxboxException {
        Iterator it = null;

        ShortMsgBean bean = new ShortMsgBean(null);
        it = bean.findBySql(sql);

        return it;
    }

    public Iterator getAllShortMsgs() throws LinuxboxException {
        return findShortMsgBySql(" ");
    }

    public Iterator getAllShortMsgs(String sort) throws LinuxboxException {
        return findShortMsgBySql(sort);
    }*/
}
